﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyBetImporter.DAL;

namespace MyBetImporter.Scores
{
    public class ClassicScore
    {

        private enum PredictionType
        {
            HomeWin, DrawGame, AwayWin
        }

        private static PredictionType GetPredictionType(int scoreHome, int scoreAway)
        {
            if (scoreHome > scoreAway)
            {
                return PredictionType.HomeWin;
            }
            else if (scoreAway > scoreHome)
            {
                return PredictionType.AwayWin;
            }
            else
            {
                return PredictionType.DrawGame;
            }
        }


        /// <summary>
        /// Detect if it's a good prediction
        /// </summary>
        /// <param name="match"></param>
        /// <param name="bet"></param>
        /// <returns></returns>
        static int ComputeGoodPrediction(Matchs match, UserBets bet)
        {
            // Good Prediction
            if ((match.ScoreAway.HasValue) && (match.ScoreHome.HasValue))
            {
                if (GetPredictionType(bet.ScoreHome, bet.ScoreAway) == GetPredictionType(match.ScoreHome.Value, match.ScoreAway.Value))
                {
                    return 1;
                }
            }
            return 0;
        }

        /// <summary>
        /// Detect if it's a good score
        /// </summary>
        /// <param name="match"></param>
        /// <param name="bet"></param>
        /// <returns></returns>
        static int ComputeGoodScore(Matchs match, UserBets bet)
        {
            if ((match.ScoreAway.HasValue) && (match.ScoreHome.HasValue))
            {
                if ((match.ScoreAway.Value == bet.ScoreAway) && (match.ScoreHome.Value == bet.ScoreHome))
                {
                    return 1;
                }
            }
            return 0;
        }

        /// <summary>
        /// Compute the score
        /// </summary>
        /// <param name="match"></param>
        /// <param name="bet"></param>
        /// <returns></returns>
        public static void ComputeScore(Matchs match, ref UserBets bet, ref Ranks rank)
        {
            rank.MatchCount += 1;
            bet.Point = 0;
            if (ComputeGoodScore(match, bet) == 1)
            {
                // Good score !
                rank.GoodPredictionCount += 1;
                rank.GoodScoreCount += 1;
                rank.TotalScore += 3 + (rank.GoodPredictionCount - 1);
                bet.Point = 3 + (rank.GoodPredictionCount - 1);
            }
            else if (ComputeGoodPrediction(match, bet) == 1)
            {
                rank.GoodPredictionCount += 1;
                rank.TotalScore += 1 + (rank.GoodPredictionCount - 1);
                bet.Point = 1 + (rank.GoodPredictionCount - 1);
            }
        }
    }
}